﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Honestysoft.Financing.Model;
using Honestysoft.Baseframework.Model;
using Honestysoft.Baseframework.WebUI;
using System.IO;

namespace Honestysoft.Financing.WebUI.Module.Project
{
    public class Utils
    {
        /// <summary>
        /// 页面之间传输树模块的参数名
        /// </summary>
        public const string ParamName_ProjectTreeModule = "TreeModule";

        /// <summary>
        /// 项目概况文档存放目录
        /// </summary>
        public const string Reports_Dir_Overview = "Reports/Project/Overview";
        /// <summary>
        /// 现场考察文档存放目录
        /// </summary>
        public const string Reports_Dir_Investigate = "Reports/Project/Investigate";
        /// <summary>
        /// 空白文档路径
        /// </summary>
        public const string Reports_Path_Blank = "Reports/blank.doc";

        /// <summary>
        /// 创建空白的项目简介
        /// </summary>
        /// <param name="dataId"></param>
        /// <param name="userId"></param>
        /// <returns></returns>
        public Sys_Attachment CreateOverviewBlank(string dataId, string userId)
        {
            return CreateBlank(dataId, userId, ((int)Honestysoft.Financing.Model.EnumItem.ProjectDocument.Overview).ToString());
        }
        /// <summary>
        /// 得到项目简介附件
        /// </summary>
        /// <param name="projectId"></param>
        /// <returns></returns>
        public Sys_Attachment GetOverview(string projectId)
        {
            //如果该项目有最新的考察报告，则使用最新的考察报告，否则使用项目简介，若无则创建新文件
            Bu_Investigate maxInvest = new Honestysoft.Financing.BusinessLogic.Project(Globals.CurrentConnStringName).GetMaxInvestigateByProject(projectId);
            List<Sys_Attachment> lsAttachment = null;
            Sys_Attachment obj = null;            
            Honestysoft.Baseframework.BusinessLogic.Attachment attLogic = new Baseframework.BusinessLogic.Attachment(Globals.CurrentConnStringName);
            if (maxInvest == null)
            {
                lsAttachment = attLogic.GetAttachmentsByDataID(projectId);
                if (lsAttachment != null && lsAttachment.Count > 0)
                {
                    obj = lsAttachment.Find((Sys_Attachment find) => find.AttachmentBuType == ((int)Honestysoft.Financing.Model.EnumItem.ProjectDocument.Overview).ToString());
                }
            }
            else
            {
                lsAttachment = attLogic.GetAttachmentsByDataID(maxInvest.ID);
                if (lsAttachment != null && lsAttachment.Count == 1)
                {
                    obj = lsAttachment[0];
                }
            }
            return obj;
        }
        /// <summary>
        /// 得到现场考察附件
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Sys_Attachment GetInvestigate(string id)
        {
            List<Sys_Attachment> lsAttachment = new Honestysoft.Baseframework.BusinessLogic.Attachment(Globals.CurrentConnStringName).GetAttachmentsByDataID(id);
            Sys_Attachment obj = null;
            if (lsAttachment != null && lsAttachment.Count == 1)
            {
                obj = lsAttachment[0];
            }
            return obj;
        }
        /// <summary>
        /// 创建空白的现场考察
        /// </summary>
        /// <param name="dataId"></param>
        /// <param name="userId"></param>
        /// <returns></returns>
        public Sys_Attachment CreateInvestigateBlank(string dataId, string userId)
        {
            return CreateBlank(dataId, userId, null);
        }

        

        /// <summary>
        /// 创建空白文档
        /// </summary>
        /// <param name="dataId"></param>
        /// <param name="userId"></param>
        /// <param name="attachmentBuType"></param>
        /// <returns></returns>
        private Sys_Attachment CreateBlank(string dataId, string userId, string attachmentBuType)
        {
            string dir = "";
            if (string.IsNullOrEmpty(attachmentBuType))
            {
                dir = Utils.Reports_Dir_Investigate;
            }
            else
            {
                dir = Utils.Reports_Dir_Overview;
            }
            string fileName = dir + "/" + dataId + ".doc";
            if (!Directory.Exists(HttpContext.Current.Server.MapPath(Globals.Home + dir)))
            {
                Directory.CreateDirectory(HttpContext.Current.Server.MapPath(Globals.Home + dir));
            }
            if (!File.Exists(HttpContext.Current.Server.MapPath(Globals.Home + fileName)))
            {
                File.Copy(HttpContext.Current.Server.MapPath(Globals.Home + Utils.Reports_Path_Blank), HttpContext.Current.Server.MapPath(Globals.Home + fileName));
            }
            Sys_Attachment obj = new Sys_Attachment();
            obj.AttachmentBuType = attachmentBuType;
            obj.CreateTime = DateTime.Now;
            obj.Creator = userId;
            obj.DataID = dataId;
            obj.Description = "";
            obj.Extension = "doc";
            obj.FileName = dataId;
            obj.ID = Honestysoft.BaseLibrary.Common.Guid.GetGuid();
            obj.Path = dir;
            obj.Size = 1;
            obj.StoreType = ((int)Honestysoft.Baseframework.Model.EnumItem.StoreType.Disk).ToString();
            return obj;
        }

        /// <summary>
        /// 项目树模块
        /// </summary>
        public enum ProjectTreeModule
        {
            /// <summary>
            /// 项目类别
            /// </summary>
            ProjectCategory,
            /// <summary>
            /// 概算
            /// </summary>
            Budget=1,
            /// <summary>
            /// 投资计划
            /// </summary>
            InvestmentPlan,
            /// <summary>
            /// 项目贷款额度
            /// </summary>
            LoanAmount,
            /// <summary>
            /// 国开行贷款额度
            /// </summary>
            GKLoanAmount,
            /// <summary>
            /// 参建单位
            /// </summary>
            Company,
            /// <summary>
            /// 新增资本金
            /// </summary>
            AddInvestment,
            /// <summary>
            /// 初始化确认
            /// </summary>
            InitConfirm,
            /// <summary>
            /// 项目概况
            /// </summary>
            Overview
        }
        /// <summary>
        /// 项目树节点类型
        /// </summary>
        public enum ProjectTreeNodeType
        {
            /// <summary>
            /// 根节点
            /// </summary>
            Root=1,
            /// <summary>
            /// 项目类别
            /// </summary>
            ProjectCategory,
            /// <summary>
            /// 项目
            /// </summary>
            Project
        }

        //public class ProjectSession
        //{
        //    /// <summary>
        //    /// Session中的项目对象
        //    /// </summary>
        //    public static BD_Project Project
        //    {
        //        get
        //        {
        //            if (HttpContext.Current.Session["Project"] != null)
        //            {
        //                return (BD_Project)HttpContext.Current.Session["Project"];
        //            }
        //            return null;
        //        }
        //        set
        //        {
        //            HttpContext.Current.Session["Project"] = value;
        //        }
        //    }
        //    /// <summary>
        //    /// 清除Session中的流程对象
        //    /// </summary>
        //    public static void Clear()
        //    {
        //        if (HttpContext.Current.Session["Project"] != null)
        //        {
        //            HttpContext.Current.Session.Remove("Project");
        //        }
        //    }
        //}
    }
}